//使用mysql原生连接
const mysql = require('mysql')
const connection = mysql.createConnection({
    host: 'localhost',
    port: '3306',
    user: 'test',
    password: 'test',
    database: 'test'
});
var table=''
db = function(name){
    table=name
    return db
}
function query(sql,values,callback){
    connection.connect((err) => {
        //if (err) throw err
        if (!err) {
            console.log('连接mysql成功')
        }
    });
    connection.query(sql, values,(err, data, field) => {
        if (err) console.log(err.message); 
        callback(err,data)
    })
    connection.end();
}
db.select=function(values,callback){  
    let sql = 'SELECT * FROM '+ table + ' WHERE id = ?'
    return query(sql,values,callback);
};
db.insert = function(values,callback){ 
    let sql = 'INSERT INTO '+ table + ' SET ?'
    return query(sql,values,callback); 
}
db.update = function(values,callback){ 
    let sql = 'UPDATE '+ table + ' SET ?'  
    return query(sql,values,callback);
}
db.remove = function(values,callback){ 
    let sql = 'DELETE FROM '+ table + ' WHERE ?' 
    return query(sql,values,callback);
}

module.exports = db
